package com.orm.mybatis.serviceImpl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.orm.mybatis.entity.User;
import com.orm.mybatis.mapper.UserMapper;
import com.orm.mybatis.utils.MD5Util;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl {

    @Resource
    private UserMapper userMapper;

    public PageInfo findAllUser(){
        PageHelper.startPage(1,10);
        List<User> list = userMapper.selectAllUser();
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public User findUserById(Long id){
        return userMapper.selectUserById(id);
    }

    public int insertUser(User user){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreateTime(new DateTime().toString(simpleDateFormat));
        user.setLastLoginTime(new DateTime().toString(simpleDateFormat));
        user.setLastUpdateTime(new DateTime().toString(simpleDateFormat));
        return userMapper.saveUser(user);
    }

    public int insertUserWithoutXml(User user){
        user.setName(user.getName()+"1");
        user.setEmail(user.getEmail()+"2");
        user.setPhoneNumber(user.getPhoneNumber()+"3");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreateTime(new DateTime().toString(simpleDateFormat));
        user.setLastLoginTime(new DateTime().toString(simpleDateFormat));
        user.setLastUpdateTime(new DateTime().toString(simpleDateFormat));
        return userMapper.saveUserWithoutXml(user);
    }

    @Transactional("localMysqlTransactionManager")
    public void test() throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date =  simpleDateFormat.format(new Date());
        User user = User.builder().email("andrew@qq.com").name("andrew").password("123456").phoneNumber("123")
                .lastUpdateTime(date).createTime(date).status(0).salt(MD5Util.createSaltValue()).build();
        try {
            testNoXml();
        }catch (Exception e) {
            e.printStackTrace();
            throw new Exception();
        }finally
         {
            System.out.println("finally");
        }
        System.out.println("还会跑吗");
        userMapper.saveUser(user);
        File file = new File("D:/车载/oookk/wawa");
        FileOutputStream outputStream = new FileOutputStream(file);


//        }finally {
//
//            System.out.println("结束");
//        }
//        try {
        System.out.println("还能继续跑吗");
            throw  new Exception();
        //        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println("bbbbbbbbb");
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
////            throw new RuntimeException();
//        }finally {
//            System.out.println("ccccccccc");
////            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }

//        if (flag>0){
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }

    }


    @Transactional("localMysqlTransactionManager")
    public void  testNoXml() throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date =  simpleDateFormat.format(new Date());
        User user = User.builder().email("test@qq.com").name("test").password("test123456").phoneNumber("test123")
                .lastUpdateTime(date).createTime(date).status(0).salt(MD5Util.createSaltValue()).build();
        try {
            int i = 1/0;
        }catch (Exception e){
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new Exception();
//         return ;
        }
        System.out.println("在testNoXml内还会跑吗");

        userMapper.saveUser(user);
        return ;
    }

    public void throwTest(){
        int a = 0;

        int k = 1/0;
        System.out.println("还能继续吗");
    }

}
